import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:get/get.dart';
import 'package:ball_lion/resources/colours.dart';
import 'package:ball_lion/utils/date_time_utils.dart';
import 'package:ball_lion/routes/routes.dart';
import 'package:ball_lion/utils/utils.dart';
import 'package:ball_lion/widgets/base/wqs_image.dart';
import 'package:ball_lion/widgets/base/wqs_text.dart';

import 'package:ball_lion/entity/ai/chart_match_entity.dart';
import 'package:ball_lion/utils/toast_utils.dart';
import 'package:ball_lion/utils/user.dart';

class ChartMatchInfo extends StatefulWidget {
  const ChartMatchInfo({super.key, required this.info});

  final ChartMatchEntity info;

  @override
  State<ChartMatchInfo> createState() => _ChartMatchInfoState();
}

class _ChartMatchInfoState extends State<ChartMatchInfo> {
  ChartMatchEntity get info => widget.info;
  String get league {
    if (info.leagueName.hasValue && info.matchTime.hasValue) {
      return "${info.leagueName} ${DateTimeUtils.formatDateString(info.matchTime!, 'MM-dd HH:mm')}";
    } else if (info.leagueName.hasValue) {
      return info.leagueName!;
    } else if (info.matchTime.hasValue) {
      return DateTimeUtils.formatDateString(
          info.matchTime!, 'yyyy-MM-dd HH:mm:ss');
    } else {
      return "";
    }
  }

  String get score90 {
    if (info.homeScore.hasValue && info.guestScore.hasValue) {
      return "${info.homeScore}-${info.guestScore}";
    } else {
      return "VS";
    }
  }

  String get half {
    if (info.homeScoreHalf.hasValue && info.guestScoreHalf.hasValue) {
      return "半 ${info.homeScoreHalf}-${info.guestScoreHalf}";
    } else {
      return "";
    }
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        if (league.hasValue)
          GestureDetector(
            onTap: () {
              if (info.leagueId.hasValue) {
                Get.toNamed(Routes.leagueDetail,
                    arguments: info.leagueId, parameters: {"type": "0"});
              }
            },
            child: QsText(
              league,
              fontSize: 12,
              color: Colours.grey66,
            ).paddingOnly(bottom: 12),
          ),
        GestureDetector(
          behavior: HitTestBehavior.opaque,
          onTap: () =>
              Get.toNamed(Routes.zqMatchDetail, arguments: info.matchId),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Expanded(child: team(info.homeLogo, info.homeName, info.homeId)),
              score(),
              Expanded(
                  child: team(info.guestLogo, info.guestName, info.guestId))
            ],
          ),
        ),
      ],
    );
  }

  Widget score() {
    return Column(
      children: [
        QsText(score90,
            fontSize: 32, fontWeight: FontWeight.w500, fontFamily: "Din"),
        if (half.hasValue)
          QsText(half, fontSize: 12, color: Colours.grey66, fontFamily: "Din"),
        GestureDetector(
          behavior: HitTestBehavior.translucent,
          onTap: () {
            if (!info.matchId.hasValue) return;
            if (User.isFollow(info.matchId ?? 0)) {
              Utils.onEvent('bsxq', params: {'bsxq': 11});
              User.unFollow(info.matchId ?? 0).then((value) {
                if (User.isFollow(info.matchId ?? 0)) {
                  Toast.text("取消关注失败");
                } else {
                  Toast.text("取消关注成功");
                }
                setState(() {});
              });
            } else {
              Utils.onEvent('bsxq', params: {'bsxq': 1});
              User.follow(info.matchId ?? 0).then((value) {
                if (User.isFollow(info.matchId ?? 0)) {
                  Toast.text("关注成功");
                } else {
                  Toast.text("关注失败");
                }
                setState(() {});
              });
            }
          },
          child: Container(
              margin: const EdgeInsets.only(top: 12),
              width: 76,
              height: 24,
              alignment: Alignment.center,
              decoration: BoxDecoration(
                  border: Border.all(
                      color: User.isFollow(info.matchId ?? 0)
                          ? Colours.greyCC
                          : Colours.main,
                      width: 0.5),
                  borderRadius: BorderRadius.circular(24.5)),
              child: QsText(User.isFollow(info.matchId ?? 0) ? "已关注" : "关注比赛",
                  fontSize: 13,
                  color: User.isFollow(info.matchId ?? 0)
                      ? Colours.greyCC
                      : Colours.main,
                  needCenter: true)),
        )
      ],
    );
  }

  Widget team(String? logo, String? name, int? id) {
    return Column(
      children: [
        QsImage(imageUrl: logo, width: 52, height: 52),
        Container(height: 4),
        SizedBox(
          height: 30 * 1.4,
          child: QsText(name,
                  textAlign: TextAlign.center,
                  fontSize: 15,
                  fontWeight: FontWeight.w500,
                  maxLines: 2)
              .paddingSymmetric(horizontal: 24),
        )
      ],
    );
  }
}
